package com.ruoyi.common.utils.push;

import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.ruoyi.common.config.JiGuangConfig;
import com.ruoyi.common.core.domain.PushBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: ruohao
 * @Date: 2023/11/16
 **/
@Service
@Slf4j
public class MyJiGuangPushServiceImpl implements MyJiGuangPushService {
    @Resource
    private JiGuangConfig jPushConfig;

    @Override
    public boolean pushAll(PushBody pushBody) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.alert(pushBody.getAlert()))
                .build());
    }

    @Override
    public boolean pushIos(PushBody pushBody) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.all())
                .setNotification(Notification.ios(pushBody.getAlert(), pushBody.getExtras()))
                .build());
    }

    @Override
    public boolean pushIos(PushBody pushBody, List<String> registerIds) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.registrationId(registerIds))
                .setNotification(Notification.ios(pushBody.getAlert(), pushBody.getExtras()))
                .build());
    }

    @Override
    public boolean pushAndroid(PushBody pushBody) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(pushBody.getAlert(), pushBody.getTitle(), pushBody.getExtras()))
                .build());
    }

    @Override
    public boolean pushAndroid(PushBody pushBody, List<String> registerIds) {
        return sendPush(PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registerIds))
                .setNotification(Notification.android(pushBody.getAlert(), pushBody.getTitle(), pushBody.getExtras()))
                .build());
    }

    @Override
    public List<String> checkRegisterIds(List<String> registerIds) {
        if (CollectionUtils.isEmpty(registerIds)) {
            return new ArrayList<>();
        }
        return registerIds.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
    }

    @Override
    public boolean sendPush(PushPayload pushPayload){
        log.info("发送极光推送请求: {}", pushPayload);
        PushResult result = null;
        try{
            result = jPushConfig.getJPushClient().sendPush(pushPayload);
        } catch (APIConnectionException e) {
            log.error("极光推送连接异常: ", e);
        } catch (APIRequestException e) {
            log.error("极光推送请求异常: ", e);
        }
        if (result!=null && result.isResultOK()) {
            log.info("极光推送请求成功: {}", result);
            return true;
        }else {
            log.info("极光推送请求失败: {}", result);
            return false;
        }
    }
}
